import { _decorator, Component, EventTouch, Label, Node, ScrollView, Sprite, SpriteFrame, UITransform } from 'cc';
import { GamerBackPack } from '../GamerBackpack';
import { Gun } from '../Items/Gun';
import { Heal } from '../Items/Heal';
import { Item } from '../Items/Item';
import { Module } from '../Items/Module';
import { Weapon } from '../Items/Weapon';
import { InGameManager } from '../Managers/InGameManager';
import { PlayerController } from '../PlayerController';
import { ControlUI } from './ControlUI';
import { PackUI } from './PackUI';
import { BundleManager } from 'db://assets/Scripts/Framework/Managers/BundleManager';
const { ccclass, property } = _decorator;

@ccclass('ItemUI')
export class ItemUI extends Component {

    @property(Sprite)
    sprite: Sprite;

    @property(Label)
    nameLabel: Label;

    @property(Label)
    numLabel: Label;

    @property(Node)
    useBtn: Node;

    item: Item;
    touchNode: Node;
    guns: Node;
    itemView: UITransform;
    dropNode: UITransform;
    pack: GamerBackPack;

    start() {
        this.node.on(Node.EventType.TOUCH_START, this.TouchStart, this);
        this.node.on(Node.EventType.TOUCH_MOVE, this.TouchMove, this);
        this.node.on(Node.EventType.TOUCH_END, this.TouchEnd, this);
        this.node.on(Node.EventType.TOUCH_CANCEL, this.TouchEnd, this);
        this.touchNode = PackUI.Instance.touchNode;
        this.itemView = PackUI.Instance.itemView;
        this.guns = PackUI.Instance.guns;
        this.pack = PlayerController.Instance.backPack;
        this.dropNode = PackUI.Instance.dropNode;
    }

    update(deltaTime: number) {

    }

    Init<T extends Item>(_item: T) {
        this.item = _item;
        BundleManager.GetBundle("MainPack").load(_item.sfUrl, SpriteFrame, (err, data) => { if (this.sprite.spriteFrame != data) this.sprite.spriteFrame = data });
        this.nameLabel.string = _item.itemName;
        this.numLabel.string = _item.num.toString();
        this.useBtn.children[0].active = _item.canUse;
        this.useBtn.children[1].active = !_item.canUse;
    }

    UseItem() {
        if (this.item instanceof Weapon) {
            if (this.pack.CurWeapon != this.item) ControlUI.Instance._WeaponChoose(this.item);
        }
        else if (this.item instanceof Heal) {
            ControlUI.Instance._UseHeal(this.item);
            PackUI.Instance.ItemInit();
        }
        else if (this.item instanceof Module) {
            PlayerController.Instance.backPack.EquipModule(this.item);
            PackUI.Instance.GunInit();
            PackUI.Instance.ItemInit();
        }
    }

    TouchStart(event: EventTouch) {
        if (PackUI.Instance.touchTarget) return;
        PackUI.Instance.touchTarget = this;
        let pos = event.getUILocation().toVec3();
        this.touchNode.setWorldPosition(pos);
    }

    TouchMove(event: EventTouch) {
        if (PackUI.Instance.touchTarget != this) return;
        let pos = event.getUILocation();
        if (!this.itemView.getBoundingBoxToWorld().contains(pos) && !this.touchNode.getComponent(Sprite).spriteFrame) {
            this.itemView.getComponent(ScrollView).enabled = false;
            this.touchNode.getComponent(Sprite).spriteFrame = this.sprite.spriteFrame;
        }
        this.touchNode.setWorldPosition(pos.toVec3());
    }

    TouchEnd(event: EventTouch) {
        if (PackUI.Instance.touchTarget != this) return;
        PackUI.Instance.touchTarget = null;
        let pos = event.getUILocation();
        this.touchNode.getComponent(Sprite).spriteFrame = null;
        this.itemView.getComponent(ScrollView).enabled = true;
        if (this.item instanceof Module) {
            let gun: Gun;
            for (let i = 0; i < this.guns.children.length; i++) {
                const element = this.guns.children[i].getComponent(UITransform);
                if (element.getBoundingBoxToWorld().contains(pos)) {
                    let num = i == 2 ? 0 : i + 1;
                    gun = this.pack.guns[num];
                    break;
                }
            }
            if (gun && gun.accessModules[this.item.type]) {
                this.pack.ModuleEquip(this.item, gun);
                PackUI.Instance.GunInit();
                PackUI.Instance.ItemInit();
                return;
            }
        }
        if (this.dropNode.getBoundingBoxToWorld().contains(pos)) {
            this.pack.Drop(this.item);
            PackUI.Instance.GunInit();
            PackUI.Instance.ItemInit();
        }
    }

    protected onDisable(): void {
        this.node.targetOff(this);
    }

}


